﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class clientLensPriceQuery : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e) {
        if (!IsPostBack) {
            CSUtils.CheckSession();
        }
    }

    protected void ButtonQuery_Click(object sender, EventArgs e)
    {
        this.binddata();
        LabelErrorMsg.Text = "";
    }

    private void binddata() {
        //客户
        string clientID = TextBoxClientID.Text;
        ClientVO vo = CSUtils.getClient(clientID);
        LabelClientInfo.Text = vo.ClientName + " [" + vo.ClientTypeName + "]";
        GridView1.DataSource = this.getAllClientLens(clientID);
        GridView1.DataBind();                
    }

    private DataSet getAllClientLens(string clientID)
    {
        string sql =
            "select min(id)as id, client_name, client_type_name, brand_name, lens_name, client_id, brand_id, lens_id from cs_client_lens_detail_view " +
            "where client_id = " + clientID + " " +
            "group by  client_name, client_type_name, brand_name, lens_name,client_id, brand_id, lens_id " +
            "union all " +
            "select min(id)as id, client_name, client_type_name, '车房片' as brand_name, lens_name, client_id, 0 as brand_id, lens_id from cs_client_custom_lens_detail_view " +
            "where client_id = " + clientID + " " +
            "group by  client_name, client_type_name, lens_name,client_id, lens_id " +
            "order by brand_id, lens_id";

        DataSet ds = new DataSet();
        DBConn myDB = new DBConn();
        ds = myDB.getDataSet(sql);
        myDB.Close();
        return ds;
    }

    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e) {
        GridView1.PageIndex = e.NewPageIndex;
        this.binddata();
    }


    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) {

        LabelErrorMsg.Text = "";

        if (e.CommandName.Equals("command_display"))
        {
            string id = e.CommandArgument.ToString();

            GridViewRow gvrow = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);   //获取被点击的linkButton所在的GridViewRow
            int index = gvrow.RowIndex;                                                         //获取到行索引 RowIndex

            //获取当前行的信息
            string brandID = GridView1.Rows[index].Cells[0].Text;
            string lensID = GridView1.Rows[index].Cells[1].Text;
            string brandName = GridView1.Rows[index].Cells[2].Text;
            string lensName = GridView1.Rows[index].Cells[3].Text;
            LabelBrandName.Text = brandName + "--";
            LabelLensName.Text = lensName;


            //string ID = e.CommandArgument.ToString();
            //ClientLensVO vo = CSUtils.getClientLens(ID);
            //LabelBrandName.Text = vo.BrandName + "--";
            //LabelLensName.Text = vo.LensName;
            //TextBoxBrandID.Text = vo.BrandID;
            //TextBoxLensID.Text = vo.LensID;
            //string brandID = vo.BrandID;
            //string lensID = vo.LensID;
            GridView2.DataSource = this.getAllLensPrice(brandID, lensID);
            GridView2.DataBind();


            int count = ((DataSet)GridView2.DataSource).Tables[0].Rows.Count;
            if (count > 0)
            {
                LabelErrorMsg2.Text = "";
            }
            else
            {
                LabelErrorMsg2.Text = "没有对应的光度信息, 请在“品牌镜片价格”中添加!";
            }
        }
        

    }

    private DataSet getAllLensPrice(string brandID, string lensID)
    {
        //客户
        string clientID = TextBoxClientID.Text;

        string sql = "";
        string where = "";

        if (brandID == "0")
        {
            sql =
                "select price.id, price.lens_id, price.sphere_begin, price.sphere_end, price.cylinder_begin, price.cylinder_end, price.sale_price, price.agent_price, " +
                "   isnull(detail.price_coef, 1.00)as price_coef, isnull(detail.add_fee,0)as add_fee " +
                "from cs_custom_lens_price_base price " +
                "left join (select * from cs_client_custom_lens_detail " +
                "   where client_id = " + clientID + " " +
                ")detail on price.id = detail.custom_lens_price_base_id " +
                "where  1=1 {0} " +
                "order by price.sphere_begin,price.sphere_end,price.cylinder_begin,price.cylinder_end";

            if (!lensID.Equals("-1"))
            {
                where += " and price.lens_id = " + lensID;
            }
        }
        else
        {
            sql =
                "select price.id, price.brand_id, price.lens_id, price.sphere_begin, price.sphere_end, price.cylinder_begin, price.cylinder_end, price.diameter, price.sale_price, price.agent_price, " +
                "   isnull(detail.price_coef, 1.00)as price_coef, isnull(detail.add_fee,0)as add_fee " +
                "from cs_lens_price_base price " +
                "left join (select * from cs_client_lens_detail " +
                "   where client_id = " + clientID + " " +
                ")detail on price.id = detail.lens_price_base_id " +
                "where  1=1 {0} " +
                "order by price.sphere_begin,price.sphere_end,price.cylinder_begin,price.cylinder_end";

            if (!brandID.Equals("-1"))
            {
                where += " and price.brand_id = " + brandID;
            }

            if (!lensID.Equals("-1"))
            {
                where += " and price.lens_id = " + lensID;
            }
        }

        sql = string.Format(sql, where);

        DataSet ds = new DataSet();
        DBConn myDB = new DBConn();
        ds = myDB.getDataSet(sql);
        myDB.Close();
        return ds;
    }

}